package org.yang.faq.service.impl;

import org.yang.faq.dao.PraiseMapper;
import org.yang.faq.model.Praise;
import org.yang.faq.model.Program;
import org.yang.faq.service.PraiseService;
import org.yang.faq.service.ProgramService;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 赞 服务实现类
 * </p>
 *
 * @author Yang
 * @since 2017-12-12
 */
@Service
public class PraiseServiceImpl extends ServiceImpl<PraiseMapper, Praise> implements PraiseService {

	@Autowired
	private ProgramService programService; 
	//点赞功能
	@Override
	public String doPraise(Praise praise) {
		Page<Praise> page = new Page<Praise>(1, 100);
		EntityWrapper wrapper = new EntityWrapper<Praise>();
		wrapper.eq("p_id", praise.getpId()).eq("wx_id", praise.getWxId()).orderBy("create_time");
		page = this.selectPage(page, wrapper);
		try {
			// 1 判断是够点赞过（pId，wxId）
			if (page.getPages() <= 0) {
				// 2 添加点赞记录
				this.insert(praise);
				// 3 更新节目表的count?
				EntityWrapper wrapper2 = new EntityWrapper<Program>();
				wrapper2.eq("id", praise.getpId());
				Program program = programService.selectOne(wrapper2);
				program.setPraiseCount(program.getPraiseCount()+1);
				programService.insertOrUpdate(program);
				return "点赞成功！";
			} else {
				return "此节目已点赞！";
			}
		} catch (Exception e) {
			return e.getMessage();
		}
	}

	 
	

}
